
#include "string.h"
#include "Motor_Data.h"
#include "Modbus.h"

#define ADDRESS_COUNT 8


Data_Table g_all_modbus_data[] = {
	
	{0xA000,(U8*)&g_set_speed,sizeof(SET_SPEED)},
	{0xB000,(U8*)&g_speed_para,sizeof(SPEED_PARA)},
	{0xC000,(U8*)&g_do_speed,sizeof(DO_SPEED)},
	
	{0xD000,(U8*)&g_motor_status,sizeof(MOTOR_STATUS)},
	{0xE000,(U8*)&g_motor_PID,sizeof(MOTOR_PID)},
	{0xF000,(U8*)&g_motor_i2t_para,sizeof(MOTOR_I2T_PARA)},
	{0xF100,(U8*)&g_motor_i2t,sizeof(MOTOR_I2T)},
	{0xF200,(U8*)&g_hall_status,sizeof(HALL_STATUS)}
	//{0xA000+sizeof(MOTOR_STATUS),(U8*)&g_run_status,sizeof(STATUS)}

};



int CheckAdd(U16 add,U16 ilen){
	char i = 0 ;
	for( i = 0 ; i< ADDRESS_COUNT ; ++i){
		if( g_all_modbus_data[i].Modbus_Add == add){
			if(ilen <= g_all_modbus_data[i].length){ 
				return i;
			}
		}
	}
	
	return -1;	
}
U8 ModbusRead(U8* p_data,U16 add,U16 ilen){
	
	int index = CheckAdd(add,ilen);	
	if(index != -1){
		memset(p_data,0,ilen);
		memcpy(p_data,g_all_modbus_data[index].real_Add,ilen);
		return ilen;
	}
	return 0;	
}
U8 ModbusWrite(U8* p_data,U16 ilen,U16 add){
	int index = CheckAdd(add,ilen);
		
	if(index != -1){
//		if( ilen > g_all_modbus_data[index].length   ){
//			ilen = g_all_modbus_data[index].length;	
//		}
		memset(g_all_modbus_data[index].real_Add,0,ilen);
		memcpy(g_all_modbus_data[index].real_Add,p_data,ilen);
		return ilen;
	}
	return 0;	
}
